<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	private function excute()
	{
		ob_start();

		switch($this->act)
		{
			case 'menu':
				$this->menu();
				break;
			case 'home':
				$this->home();
				break;
			default:
				$this->main();
				break;
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}

	private function main()
	{		
		$menuList = $this->menu();
		include template('admin/index');
	}

	private function menu()
	{
		$default_open = true;
		$open_onlyone = false;
		$open_list = isset($this->request['open']) ? explode('_', $this->request['open']) : '';

		require ROOT_PATH.'config/menu_list_admin.php';

		foreach ($menu_list as $_key=>$_menu)
		{
			if(isset($_menu['sub_menu_list']) && check_array($_menu['sub_menu_list']))
			{
				foreach ($_menu['sub_menu_list'] as $_sub_key=>$_sub_menu)
				{
					if(strpos($_sub_menu['link'],":\/\/")!==false)continue;

					preg_match("~m=([^&\x23]+)&?(a=([^&\x23]*))?~", $_sub_menu['link'],$match);
						
					if(is_array($match) && count($match) == 4)
					{
						list(,$_mod,,$_code) = $match;
					}
				}
			}
		}

		$all_open_list = array_keys($menu_list);
		if($default_open && isset($this->request['open'])==false)
		{
			$open_list = $all_open_list;
		}

		foreach($menu_list as $key=>$menu)
		{
			if ($key == 1)
			{
				foreach ($menu_list as $_menu_list_s)
				{
					foreach((array)$_menu_list_s['sub_menu_list'] as $menu_s)
					{
						if($menu_s['shortcut'])
						{
							$menu['sub_menu_list'][] = $menu_s;
						}
					}
				}
			}

			if(empty($menu['sub_menu_list']))continue;

			$menu_tmp_list[$key]=$menu;

			if(in_array($key,$open_list)!=false)
			{
				$menu_tmp_list[$key]['img']='minus';
				$open_list_tmp=$open_list;
				unset($open_list_tmp[array_search($key, $open_list_tmp)]);
			}
			else
			{
				$menu_tmp_list[$key]['img']='plus';
				$menu_tmp_list[$key]['sub_menu_list']=array();
			}

			if(isset($menu['sub_menu_list']))
			{
				$menu_tmp_list[$key]['link']="?mod=index&code=menu";
				$menu_tmp_list[$key]['target']="";
			}
			else
			{
				$menu_tmp_list[$key]['target'] = 'target="main"';
			}
		}

		$menu_list = $menu_tmp_list;

		return $menu_list;
	}

	private function home()
	{
		include ROOT_PATH.'config/menu_list_admin.php';
		$shorcut_list = array();
		foreach($menu_list as $_menu_list)
		{
			foreach((array)$_menu_list['sub_menu_list'] as $menu)
			{
				if($menu['shortcut'])
				{
					$shortcut_list[$_menu_list['title']][] = $menu;
				}
			}
		}

		include template('admin/home');
	}
}